package com.wswy.wzcx.module.validator;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.utils.optional.function.Consumer;
import com.tencent.captchasdk.TCaptchaDialog;
import com.tencent.captchasdk.TCaptchaVerifyListener;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.api.Configs;
import com.wswy.wzcx.model.resp.CaptchaValidData;
import kotlin.random.Random;
import kotlin.random.RandomKt;
import org.json.JSONObject;

public class TCaptchaValidator implements IValidator{


  private Activity activity;

  private TCaptchaDialog dialog;

  public TCaptchaValidator(Activity activity) {
    this.activity = activity;
  }

  private String randomAppId(){
    int idx = Random.Default.nextInt(0,Configs.TCAPTCHA_APPS.length);
    return Configs.TCAPTCHA_APPS[idx];
  }

  @Override
  public void startVerify(final Consumer<ValidatorResult> ticketConsumer) {
    dialog = new TCaptchaDialog(activity, true, new OnCancelListener() {
      @Override
      public void onCancel(DialogInterface dialog) {

        if (ticketConsumer != null){
          ticketConsumer.consume(null);
        }
      }
    },randomAppId(),
        new TCaptchaVerifyListener() {
          @Override
          public void onVerifyCallback(JSONObject jsonObject) {
            int ret = jsonObject.optInt("ret", -1);

            ValidReq validReq=null;
            if (ret == 0) {
              //验证成功回调
              String ticket = jsonObject.optString("ticket", null);
              String appId = jsonObject.optString("appid", null);
              String randstr = jsonObject.optString("randstr", null);
              if (!TextUtils.isEmpty(ticket) && !TextUtils.isEmpty(appId)  &&  !TextUtils.isEmpty(randstr)) {

                validReq = new ValidReq(IValidator.TC,null,null,ticket,appId,randstr);

              }
            } else if (ret == -1001) {

              //验证码首个TCaptcha.js加载错误，业务可以根据需要重试

              String info = jsonObject.optString("info");

            } else {

              //验证失败回调，一般为用户关闭验证码弹框
            }

            if (validReq != null && validReq.getType() != null){
              Api.get().validCaptcha(validReq).subscribe(new ApiOptionalResultObserver<CaptchaValidData>(){

                @Override
                protected void onApiResult(@Nullable CaptchaValidData captchaValidData) {
                  ValidatorResult result = null;
                  if (captchaValidData != null && captchaValidData.getTicket() != null){
                    result = new ValidatorResult(IValidator.TC,captchaValidData.getTicket());
                  }
                  if (ticketConsumer != null) {
                    ticketConsumer.consume(result);
                  }
                }

                @Override
                protected void onApiError(@Nullable BaseResult result) {
                  if (ticketConsumer != null) {
                    ticketConsumer.consume(null);
                  }
                }
              });
            }else {
              if (ticketConsumer != null) {
                ticketConsumer.consume(null);
              }
            }

          }
        }, null);
    dialog.show();
  }

  @Override
  public void onDestroy() {

    if (dialog != null && dialog.isShowing()){
      dialog.dismiss();
    }

  }
}
